package com.pricl.biz.upload.manager;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.pricl.biz.file.entity.FileManager;
import com.pricl.biz.file.service.FileManagerService;
import com.pricl.biz.system.entity.SystemConfig;
import com.pricl.biz.system.service.SystemConfigService;
import com.pricl.frame.core.constants.FileManagerConstants;

@Component("systemConfigManager")
public class SystemConfigManager {
	/**
	 * 属性配置文件服务类
	 */
	@Autowired
	private SystemConfigService systemConfigService;

	/**
	 * 文件管理对象服务类
	 */
	@Autowired
	private FileManagerService fileManagerService;

	/**
	 * 根据配置唯一的CODE值查找
	 * 
	 * @param configCode
	 *            配置的唯一code
	 * @return
	 */
	public SystemConfig getSystemConfigByConfigCode(String configCode) {
		//String redisKey = RedisKeyUtils.systemConfig(configCode);
		//SystemConfig entity = get(redisKey, SystemConfig.class);
		SystemConfig entity = systemConfigService.selectUniqueByConfigCode(configCode);
/*		
		if(entity == null){
			entity = systemConfigService.selectUniqueByConfigCode(configCode);
			if(entity != null){
				super.set(redisKey, entity);
			}
		}
*/		
		return entity;
	}

	/**
	 * 根据文件id，获取完成的访问路径
	 * 
	 * @param fileSid
	 * @return
	 */
	public String getFullAccessFilePath(Long fileSid) {
		String rootUrl = getAccessRootPath();
		FileManager filemanager = this.fileManagerService.selectByPrimaryKey(fileSid);
		if(filemanager != null && !StringUtils.isEmpty(filemanager.getFileUrl())){
			return rootUrl + filemanager.getFileUrl();
		}
		return null;
	}

	/**
	 * 获取完整的访问根路径
	 * 
	 * @return
	 */
	public String getAccessRootPath() {
		String rootUrl = getSystemConfigValueByConfigCode(FileManagerConstants.FILE_UPLOAD_ACCESS_ROOT_URL);
		return rootUrl;
	}
	
	/**
	 * 获取完整的阿里云OSS访问根路径
	 * 
	 * @return
	 */
	public String getOssAccessRootPath() {
		String rootUrl = getSystemConfigValueByConfigCode(FileManagerConstants.FILE_UPLOAD_ACCESS_ROOT_OSS_URL);
		return rootUrl;
	}
	
	/**
	 * 获取完整的上传根路径
	 * 
	 * @return
	 */
	public String getUploadRootPath() {
		String rootUrl = getSystemConfigValueByConfigCode(FileManagerConstants.FILE_UPLOAD_DIRECTORY);
		return rootUrl;
	}

	/**
	 * 根据配置的CODE，查找值
	 * 
	 * @param configCode
	 * @return
	 */
	public String getSystemConfigValueByConfigCode(String configCode) {
		SystemConfig entity = getSystemConfigByConfigCode(configCode);
		if (entity == null)
			return "";
		return entity.getConfigValue();
	}
}
